﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Library.Data.Security.Encryption;

namespace UnigenReport.Form
{
    public partial class ucChangePass : UserControl
    {    
        #region Thiết lập

        public ucChangePass()
        {
            InitializeComponent();
            SetEvent();
        }

        /// <summary>
        /// Khai báo các sự kiện
        /// </summary>
        private void SetEvent()
        {
            this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
            this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
        }

        /// <summary>
        /// Thiết lập các giá trị mặc định của form
        /// </summary>
        private void SetDefault()
        {
        }

        #endregion

        #region Hàm

        /// <summary>
        /// Lưu mật khẩu mới
        /// </summary>
        private void Save()
        {
            try
            {
                var oldPass = txtOldPass.Text.Trim();
                var newPass1 = txtNewPass1.Text.Trim();
                var newPass2 = txtNewPass2.Text.Trim();

                #region Check valid
                if (String.IsNullOrEmpty(oldPass) || String.IsNullOrEmpty(newPass1) || String.IsNullOrEmpty(newPass2))
                {
                    Common.ShowNoticeMess("Please fill the require fields!");
                    return;
                }

                if (MD5Encryptor.Encrypt(Variables.UserName.ToUpper(), oldPass) != Variables.Password)
                {
                    Common.ShowNoticeMess("The old password does not match!");
                    return;
                }

                if (newPass1 != newPass2)
                {
                    Common.ShowNoticeMess("New passwords do not match!");
                    return;
                }
                #endregion

                //Lấy thông tin
                var db = new UnigenDataContext();
                var user = db.Users.SingleOrDefault(p => p.UserID == Variables.UserID);

                if (user == null)
                {
                    Common.ShowNoticeMess("Cannot found the account!");
                    return;
                }

                //Lưu lại mật khẩu mới
                user.Password = MD5Encryptor.Encrypt(Variables.UserName.ToUpper(), newPass1);
                db.SubmitChanges();

                //Clear form
                txtOldPass.Text = txtNewPass1.Text = txtNewPass2.Text = String.Empty;
                Variables.Password = user.Password;

                //Thông báo
                Common.ShowDoneMess("Password successfully changed!");
            }
            catch (Exception ex)
            {
                Common.ShowErrorMess("Save - " + ex.Message);
            }
        }

        /// <summary>
        /// Hủy
        /// </summary>
        private void Cancel()
        {
            (this.Parent as Telerik.WinControls.UI.RadPageViewPage).Dispose();
        }
        #endregion

        #region Sự kiện

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.Enter && btnSave.Enabled == true && !btnSave.Focused && !btnCancel.Focused)
            {
                Save();
            }
            if (keyData == Keys.Escape && !btnCancel.Focused)
            {
                Cancel();
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            Save();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Cancel();
        }

        #endregion

    }
}
